﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

using Global.Class.Library;

namespace DataTransferObject
{
    public class InMarketDetail : NotifyPropertyChangeObject
    {
        private int collectedTypeID;

        private int commodityID;
        private double quantity;

        private double uprice;
        private double upriceEX;
        private double upriceOD;
        private double upriceCO;

        private double discountPercent;

        private string remarks;


        public InMarketDetail()
            : this(0, 0, 0, 0, 0, 0, 0, 0, "")
        {
        }

        public InMarketDetail(int collectedTypeID, int commodityID, double quantity, double uprice, double upriceEX, double upriceOD, double upriceCO, double discountPercent, string remarks)
        {
            GlobalDefaultValue.Apply(this);

            this.CollectedTypeID = collectedTypeID;

            this.CommodityID = commodityID;
            this.Quantity = quantity;

            this.UPrice = uprice;
            this.UPriceEX = upriceEX;
            this.UPriceOD = upriceOD;
            this.UPriceCO = upriceCO;

            this.DiscountPercent = discountPercent;

            this.Remarks = remarks;
        }


        #region Properties

        [DefaultValue(0)]
        public int CollectedTypeID
        {
            get { return this.collectedTypeID; }
            set { ApplyPropertyChange<InMarketDetail, int>(ref this.collectedTypeID, o => o.CollectedTypeID, value); }
        }


        [DefaultValue(0)]
        public int CommodityID
        {
            get { return this.commodityID; }
            set { ApplyPropertyChange<InMarketDetail, int>(ref this.commodityID, o => o.CommodityID, value); }
        }


        [DefaultValue(0)]
        public double Quantity
        {
            get { return this.quantity; }
            set { ApplyPropertyChange<InMarketDetail, double>(ref this.quantity, o => o.Quantity, Math.Round(value, GlobalVariables.Round2Quantity)); }
        }


        [DefaultValue(0)]
        public double UPrice
        {
            get { return this.uprice; }
            set { ApplyPropertyChange<InMarketDetail, double>(ref this.uprice, o => o.UPrice, Math.Round(value, GlobalVariables.Round2CUR)); }
        }


        [DefaultValue(0)]
        public double UPriceEX
        {
            get { return this.upriceEX; }
            set { ApplyPropertyChange<InMarketDetail, double>(ref this.upriceEX, o => o.UPriceEX, Math.Round(value, GlobalVariables.Round2CUR)); }
        }


        [DefaultValue(0)]
        public double UPriceCO
        {
            get { return this.upriceCO; }
            set { ApplyPropertyChange<InMarketDetail, double>(ref this.upriceCO, o => o.UPriceCO, Math.Round(value, GlobalVariables.Round2CUR)); }
        }

        [DefaultValue(0)]
        public double UPriceOD
        {
            get { return this.upriceOD; }
            set { ApplyPropertyChange<InMarketDetail, double>(ref this.upriceOD, o => o.UPriceOD, Math.Round(value, GlobalVariables.Round2CUR)); }
        }

        [DefaultValue(0)]
        public double DiscountPercent
        {
            get { return this.discountPercent; }
            set { ApplyPropertyChange<InMarketDetail, double>(ref this.discountPercent, o => o.DiscountPercent, Math.Round(value, GlobalVariables.Round5Discount)); }
        }

        public double UPriceDiscount
        {
            get { return Math.Round(this.UPrice - Math.Round(this.UPrice * this.DiscountPercent / 100, GlobalVariables.Round2CUR), GlobalVariables.Round2CUR); }
        }





        public double AmountPrimitive
        {
            get { return Math.Round(this.Quantity * this.UPrice, GlobalVariables.Round2CUR); }
        }

        [DefaultValue(0)]
        public double AmountDiscount
        {
            get { return Math.Round(this.Quantity * Math.Round(this.UPrice * this.DiscountPercent / 100, GlobalVariables.Round2CUR), GlobalVariables.Round2CUR); }
        }


        [DefaultValue(0)]
        public double AmountActual
        {
            get { return Math.Round(this.Quantity * this.UPriceDiscount, GlobalVariables.Round2CUR); }
        }



        [DefaultValue("")]
        public string Remarks
        {
            get { return this.remarks; }
            set { ApplyPropertyChange<InMarketDetail, string>(ref this.remarks, o => o.Remarks, value); }
        }

        #endregion

        protected override System.Collections.Generic.List<ValidationRule> CreateRules()
        {
            List<ValidationRule> validationRules = base.CreateRules();
            validationRules.Add(new SimpleValidationRule("CommodityID", "Please fill a commodity.", delegate { return (this.CommodityID > 0); }));
            validationRules.Add(new SimpleValidationRule("Quantity", "Quantity must be greater than zero.", delegate { return (this.Quantity > 0); }));

            return validationRules;
        }
    }
}
